package com.sxyjhh.common;

import com.sxyjhh.dbutil.basic.DBQueryUtils;
import com.sxyjhh.utils.util.LoadJsonMapping;
import org.springframework.stereotype.Component;

import java.sql.SQLException;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by Administrator on 2018/1/3.
 */
@Component
public class DBConfig {

    private String ip;//	数据库ip地址

    private String port;//	端口

    private String password = readPop("spring.datasource.password");//	数据库密码
    private String username = readPop("spring.datasource.username");//	数据库登陆用户
    private String dbtype;//	数据库类型
    private String dbname;//	数据库实例名称

    private String url = readPop("spring.datasource.url");//URL

    private String PConnectUrl = readPop("daq.task.pconnect.url");

    private String commitSize = readPop("daq.task.commit.size");

    private String batchSize = readPop("daq.task.batch.size");

  /*  public final static BlockingQueue<String> QYXY_CONTROL = new LinkedBlockingQueue<String>();

    public final static BlockingQueue<String> AQRY_CONTROL = new LinkedBlockingQueue<String>();
*/
    public static String readPop(String jsonName) {

        LoadJsonMapping resourceLoader = new LoadJsonMapping("application.properties");
        String pop = resourceLoader.getPropertiesValue(jsonName);

        return pop;

    }

    public static List<Object> getKey(DBQueryUtils dbQueryUtils, String table, String type, String dbName, String nameSpace) {

        if (!dbQueryUtils.getConnectStatus()) {
            return null;
        }

        StringBuffer sql = new StringBuffer();
        if ("1".equals(type)) {
            sql.append(" select cu.COLUMN_NAME from all_cons_columns cu, all_constraints au " +
                    "where cu.constraint_name = au.constraint_name and au.constraint_type = 'P' " +
                    "and au.table_name ='" + table + "'  AND cu.OWNER='" + nameSpace + "'");
        }

        if ("3".equals(type)) {
            sql.append(" SELECT k.column_name FROM information_schema.table_constraints t " +
                    "JOIN information_schema.key_column_usage k USING " +
                    "(constraint_name,table_schema,table_name) WHERE t.constraint_type='PRIMARY KEY'" +
                    " AND t.table_schema='" + dbName + "' AND t.table_name='" + table + "'");
        }



        if ("4".equals(type)) {
            sql.append(" SELECT SYSCOLUMNS.name AS column_name " +
                    " FROM SYSCOLUMNS,SYSOBJECTS,SYSINDEXES,SYSINDEXKEYS " +
                    " WHERE SYSCOLUMNS.id = object_id('"+table+"') " +
                    " AND SYSOBJECTS.xtype = 'PK' " +
                    " AND SYSOBJECTS.parent_obj = SYSCOLUMNS.id " +
                    " AND SYSINDEXES.id = SYSCOLUMNS.id " +
                    " AND SYSOBJECTS.name = SYSINDEXES.name " +
                    " AND SYSINDEXKEYS.id = SYSCOLUMNS.id " +
                    " AND SYSINDEXKEYS.indid = SYSINDEXES.indid " +
                    " AND SYSCOLUMNS.colid = SYSINDEXKEYS.colid ");
/*
        if ("4".equals(type)) {
            sql.append("SELECT " +
                    " OBJECT_NAME(sysobjects.parent_obj) tableName," +
                    " syscolumns.name " +
                    " AS column_name  " +
                    " FROM sysobjects INNER JOIN sysconstraints  " +
                    " ON  sysobjects.xtype in( 'PK' )   AND  " +
                    " sysobjects.id " +
                    " = sysconstraints.constid  " +
                    " inner JOIN syscolumns ON sysconstraints.id " +
                    " = syscolumns.id " +
                    " WHERE syscolumns.typestat=3 " +
                    "AND  OBJECT_NAME(sysobjects.parent_obj)='"+table+"'");
*/
        }

        List<Object> keyList = null;
        try {
            keyList = dbQueryUtils.queryData(sql.toString());
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return keyList;
    }
}
